package org.itboys.ace.web.lasted;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.admin.entity.AdminOrg;
import org.itboys.admin.entity.lasted.SystemOrg;
import org.itboys.admin.service.AdminOrgService;
import org.itboys.admin.service.lasted.SystemOrgService;
import org.itboys.admin.tools.AdminPermissionCheck;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
/**
 *作者：江雄
 * 描述：系统-组织
 * 时间：2017年2月24日.重新构建
 */
@RestController
@RequestMapping("/ace/system/org")
public class AceSystemOrgController extends BaseController {
	@Autowired
	private SystemOrgService orgService;

	/**
	 * 点击部门管理跳转页面
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("/org-list")
	public ModelAndView listvm(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return new ModelAndView("/ace/sys/org/org-list");
	}
	/***
	 * 查询所有列表数据(2017年2月22日，树结构)
	 *
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("/getTree")
	@AdminPermissionCheck("getTree")
	public void getTree(HttpServletRequest request, HttpServletResponse response,
					 Model model) {
		List<SystemOrg> orgList = orgService.getOrgTree();
		AjaxUtils.renderJson(response, orgList);
	}

	/***
	 * 根据id查询组织信息
	 *
	 * @param id
	 * @param response
	 */
	@RequestMapping("/findOne")
	public void findOne(@RequestParam(value="id",required = true)Long id, HttpServletResponse response) {
		AjaxUtils.renderJson(response, orgService.getById(id));
	}
	/***
	 * 保存部门菜单列表
	 * 
	 * @param systemOrg
	 * @param response
	 */
	@ResponseBody
	@RequestMapping("/save")
	public void save(@ModelAttribute SystemOrg systemOrg,
			HttpServletResponse response) {

		try {
			orgService.saveOrg(systemOrg);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("adminOrg save error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}


	/**
	 * 根据id删除列表
	 * 
	 * @param id
	 * @param response
	 */
	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable("id") Long id, HttpServletResponse response) {
		try {

			orgService.deleteOrg(id);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("adminOrg delete error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	/**
	 * 组织选择器，
	 */
	/**
	 * 质押物选择器
	 * @param request
	 * @param isSingle
	 * @param param
	 * @param model
	 * @return
	 */
	@RequestMapping("/choose")
	public ModelAndView choose(HttpServletRequest request,
							   HttpServletResponse response,
							   @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
							   @RequestParam(value="param",required=false)String param,//查询参数,
							   @RequestParam(value="callBack",required=false)String callBack,//回调函数
							   Model model) {
		//return new ModelAndView("/ace/sys/org-list");
		model.addAttribute("param",param);
		model.addAttribute("callBack",callBack);
		if(isSingle==null){
			return  new ModelAndView("/ace/sys/org/choose_erro");
		}else if(isSingle==1){
			return  new ModelAndView("/ace/sys/org/choose_single");
		}else if(isSingle==0){
			return  new ModelAndView("/ace/sys/org/choose_muilt");
		}

		return  new ModelAndView("/ace/sys/org/choose_erro");
	}
/*	@RequestMapping("/choose" )
	public  String choose(HttpServletRequest request,
								@RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
								@RequestParam(value="param",required=false)String param,//查询参数
								Model model){
		model.addAttribute("param",param);
		if(isSingle==null){
			return "/ace/sys/org/choose_erro";
		}else if(isSingle==1){
			return "/ace/sys/org/choose_single";
		}else if(isSingle==0){
			return "/ace/sys/org/choose_muilt";
		}

		return "/ace/sys/org/choose_erro";
	}*/
}
